﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using DotNetUI.Controls;

using Microsoft.Extensions.Options;

using Xc.DataAccess.Core.Services;
using Xc.DataAccess.HyperTerminalCore.Models;
using Xc.DataAccess.HyperTerminalCore.Services;
using Xc.Views.Core;

namespace Xc.Logics.HyperTerminalCore.Tcp.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainView : UserControl
    {
        private readonly IApplicationBooster<HyperAppSetting> startUp;
        private readonly ITcpServerFactory tcpServerFactory;
        private readonly IOptions<HyperAppSetting> options;
        private readonly IEncodingService encoding;
        public MainView(ITcpServerFactory tcpServerFactory, IOptions<HyperAppSetting> options, IEncodingService encoding, IApplicationBooster<HyperAppSetting> startUp)
        {
            InitializeComponent();
            this.ViewModel.Port = options.Value.Port;
            this.tcpServerFactory = tcpServerFactory;
            this.options = options;
            this.encoding = encoding;
            this.startUp = startUp;
            this.ViewModel.Port=options.Value.Port;
        }

        private async void DotNetButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel.Port == 0)
            {
                MessageBox.Show("请输入有效的端口号", "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            var respond = await tcpServerFactory.SetServiceAsync(new[] { new TcpServerOption { FunctionName = $"TCP{this.ViewModel.TcpServerOptions.Count}", ServiceIndex = this.ViewModel.TcpServerOptions.Count, Port = this.ViewModel.Port,Encoding= encoding.GetGB2312() } });
            if (respond.FirstOrDefault()?.IsError == true)
            {
                MessageBox.Show(respond.FirstOrDefault()?.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                options.Value.Port = this.ViewModel.Port;
                startUp.UpdateAppSettingFile(options.Value);
                var svc = respond.FirstOrDefault()?.Infos;
                svc.Dispatcher = this.Dispatcher;
                svc.OnClientCloing += Svc_OnCloing;
                this.ViewModel.TcpServerOptions.Add(svc);
                this.ViewModel.Port++;
            }
        }

        private void Svc_OnCloing(TcpClientInfo obj)
        {
            this.Dispatcher.Invoke(() => {
                var item = _tab.Items.OfType<DotNetTableItem>().Where(p => p.Header?.ToString() == obj.IpAddress).FirstOrDefault();

                if (item != null)
                {
                    _tab.Items.Remove(item);
                }
            });

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button)
            {
                if (button.DataContext is ITcpServerService service)
                {
                    foreach (var item in service.TcpClients)
                    {
                        item.Close();
                        var d = _tab.Items.OfType<DotNetTableItem>().Where(p => p.Header?.ToString() == item.IpAddress).FirstOrDefault();
                        if (d != null)
                        {
                            _tab.Items.Remove(d);
                        }
                    }
                    service.Close();
                    this.ViewModel.TcpServerOptions.Remove(service);
                }
            }
        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            if (sender is Button button)
            {
                if (button.DataContext is TcpClientInfo service)
                {
                    service.Close();
                    Svc_OnCloing(service);
                }
            }
        }

        private void _btn_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button)
            {
                if (button.DataContext is TcpClientInfo service)
                {
                    var item = _tab.Items.OfType<DotNetTableItem>().Where(p => p.Header?.ToString() == service.IpAddress).FirstOrDefault();

                    if (item == null)
{
                        _tab.Items.Add(new DotNetTableItem { DataContext = service, Header = service.IpAddress, IsSelected = true,Content=new TerminalView(service) });
                    }
                    else
                    {
                        item.IsSelected = true;
                    }
                }
            }
        }
    }
}
